package com.github.ihsg.demo.ui.whole

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.github.ihsg.demo.R
import com.zzh.lib.pin.KeyboardWidget
import com.zzh.lib.pin.PayEditText


class PinActivity : AppCompatActivity() {
    /*private val KEY = arrayOf(
        "1", "2", "3",
        "4", "5", "6",
        "7", "8", "9",
        "<<", "0", "完成"
    )*/

    private val KEY = arrayOf("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "<<", "完成")

    lateinit var payEditText: PayEditText
    lateinit var keyboard: KeyboardWidget

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pin)
        initView()
        setSubView()
        initEvent()
    }

    private fun initView() {
        payEditText = findViewById<View>(R.id.PayEditText_pay) as PayEditText
        keyboard = findViewById<View>(R.id.KeyboardView_pay) as KeyboardWidget
    }

    private fun setSubView() {
        //设置键盘
        keyboard.setNumberKeyboard()
    }

    private fun initEvent() {
        keyboard.setOnClickKeyboardListener { position, value ->
            if (position < 11 && position != 9) {
                payEditText.add(value)
            } else if (position == 9) {
                payEditText.remove()
            } else if (position == 11) {
                //当点击完成的时候，也可以通过payEditText.getText()获取密码，此时不应该注册OnInputFinishedListener接口
                Toast.makeText(application, "您的密码是：" + payEditText.text, Toast.LENGTH_SHORT).show()
                finish()
            }
        }
        /**
         * 当密码输入完成时的回调
         */
        payEditText.setOnInputFinishedListener { password ->
            Toast.makeText(
                application,
                "您的密码是：$password",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    companion object {

        fun startAction(context: Context) {
            val intent = Intent(context, PinActivity::class.java)
            context.startActivity(intent)
        }
    }
}